package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.zhijian.medical.dao.local.CnasNewFileMapper;
import com.zhijian.medical.entity.po.CnasNewFile;
import com.zhijian.medical.entity.vo.request.CnasNewFileSearchVo;
import com.zhijian.medical.entity.vo.request.CnasNewFileSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.CnasNewFileVo;
import com.zhijian.medical.entity.vo.response.NewFileVo;
import com.zhijian.medical.service.CnasNewFileService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * cnas文件关系表业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2025-06-17 11:02:59
 */
@Slf4j
@Service
public class CnasNewFileServiceImpl implements CnasNewFileService {

	@Autowired
    private CnasNewFileMapper mapper;

    @Override
    public CnasNewFile selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public CnasNewFileVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(CnasNewFile cnasNewFile) {
        Date now = new Date();
        cnasNewFile.setCreateTime(now);
        cnasNewFile.setUpdateTime(now);
        return mapper.insert(cnasNewFile) > 0 ? cnasNewFile.getId() : 0L;
    }

    @Override
    public Long insertSelective(CnasNewFile cnasNewFile) {
        Date now = new Date();
        cnasNewFile.setCreateTime(now);
        cnasNewFile.setUpdateTime(now);
        return mapper.insertSelective(cnasNewFile) > 0 ? cnasNewFile.getId() : 0L;
    }

    @Override
    public boolean update(CnasNewFile cnasNewFile) {
        Date now = new Date();
        cnasNewFile.setUpdateTime(now);
        return mapper.updateByPrimaryKey(cnasNewFile) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(CnasNewFile record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(CnasNewFileSaveOrUpdateVo saveOrUpdateVo) {
        // 删除所有旧数据
        CnasNewFileSearchVo searchVo = new CnasNewFileSearchVo();
        BeanUtils.copyProperties(saveOrUpdateVo, searchVo);
        searchVo.setFileIds(null);
        searchVo.setFileId(null);
        deleteByCondition(searchVo);
        // 插入新数据
        if (CollUtil.isNotEmpty(saveOrUpdateVo.getFileIds())) {
            // 批量
            Date now = new Date();
            List<CnasNewFile> cnasNewFiles = new ArrayList<>();
            for (Long id : saveOrUpdateVo.getFileIds()) {
                CnasNewFile cnasNewFile = new CnasNewFile();
                BeanUtils.copyProperties(saveOrUpdateVo, cnasNewFile);
                cnasNewFile.setFileId(id);
                cnasNewFile.setCreateTime(now);
                cnasNewFile.setUpdateTime(now);
                cnasNewFiles.add(cnasNewFile);
            }
            return insertBatch(cnasNewFiles);
        } else {
            CnasNewFile cnasNewFile = new CnasNewFile();
            BeanUtils.copyProperties(saveOrUpdateVo, cnasNewFile);
            return this.insert(cnasNewFile) != 0;
        }

    }

    @Override
    public boolean modify(CnasNewFileSaveOrUpdateVo saveOrUpdateVo) {
        CnasNewFile old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        CnasNewFile cnasNewFile = new CnasNewFile();
        BeanUtils.copyProperties(saveOrUpdateVo, cnasNewFile);
        return this.updateByPrimaryKeySelective(cnasNewFile);
    }

    @Override
    public CnasNewFileVo selectOne(CnasNewFileSearchVo cnasNewFileSearchVo) {
        return mapper.selectOne(cnasNewFileSearchVo);
     }

    @Override
    public Integer count(CnasNewFileSearchVo cnasNewFileSearchVo) {
        return mapper.count(cnasNewFileSearchVo);
    }

    @Override
    public List<CnasNewFileVo> selectList(CnasNewFileSearchVo cnasNewFileSearchVo) {
        return mapper.selectList(cnasNewFileSearchVo);
    }

    @Override
    public Page<CnasNewFileVo> selectPage(CnasNewFileSearchVo cnasNewFileSearchVo) {
        AssertUtil.check(Objects.isNull(cnasNewFileSearchVo.getPageNum()) || Objects.isNull(cnasNewFileSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(cnasNewFileSearchVo);
        //分页
        Page<CnasNewFileVo> page = new Page<>(true, count, cnasNewFileSearchVo.getPageNum(), cnasNewFileSearchVo.getPageSize());
        cnasNewFileSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<CnasNewFileVo> responseVos = this.selectList(cnasNewFileSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<CnasNewFile> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(CnasNewFileSearchVo cnasNewFileSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(cnasNewFileSearchVo);
    }


    @Override
    public List<NewFileVo> selectFileList(CnasNewFileSearchVo cnasNewFileSearchVo) {
        return mapper.selectFileList(cnasNewFileSearchVo);
    }
}